package com.ailanyin.admin.controller;

import com.ailanyin.admin.service.SysBlacklistUserService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.ailanyin.common.utils.ExcelUtil;
import com.ailanyin.model.SysBlacklistUser;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/11/1 0001 下午 15:12
 */
@RestController
@RequestMapping("/system/blacklist")
@Api(tags = "后台黑名单管理")
public class SysBlacklistUserController {

    @Autowired
    private SysBlacklistUserService sysBlacklistUserService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:blacklist:query')")
    @ApiOperation(value = "获取黑名单列表")
    public CommonPage list(SysBlacklistUser blacklistUser,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysBlacklistUser> list = sysBlacklistUserService.selectBlackList(blacklistUser);
        return CommonPage.restPage(list);
    }


    @GetMapping("/export")
    @PreAuthorize("hasAuthority('system:blacklist:export')")
    @ApiOperation(value = "导出黑名单")
    public AjaxResult export(SysBlacklistUser blacklistUser) {
        List<SysBlacklistUser> list = sysBlacklistUserService.selectBlackList(blacklistUser);
        try {
            return AjaxResult.success(ExcelUtil.exportExcel(list, "黑名单表", "黑名单", SysBlacklistUser.class, "黑名单"));
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/remove")
    @PreAuthorize("hasAuthority('system:blacklist:remove')")
    @ApiOperation(value = "批量删除黑名单")
    public AjaxResult removeBlacklistUser(@RequestBody List<SysBlacklistUser> ids) {
        sysBlacklistUserService.removeBlacklistUser(ids);
        return AjaxResult.success();
    }
}
